﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kaiser.Infrastructure.BootStrapperTask;
using Kaiser.Infrastructure.InversionOfControl;
using Kaiser.Infrastructure;
using System.Web.Routing;
using System.Web.Mvc;
using Kaiser.Configuration;
using Kaiser.FilterProviders;

namespace Kaiser.BootStrapperTasks
{
    /// <summary>
    /// 引导程序，用于加载模块
    /// </summary>
    public class LoadModules : IBootStrapperTask
    {
        private readonly IDependencyInjector _dependencyInjector;

        public LoadModules(IDependencyInjector dependencyInjector)
        {
            this._dependencyInjector = dependencyInjector;
        }

        #region IBootStrapperTask Members

        /// <summary>
        /// 执行引导程序
        /// </summary>
        /// <param name="bootStrapperState">引导程序状态</param>
        public void Execute(IDictionary<string, object> bootStrapperState)
        {
            //已经存在的单例
            IModuleRegistry moduleRegistry = this._dependencyInjector.GetService<IModuleRegistry>();
            RouteCollection routes = this._dependencyInjector.GetService<RouteCollection>();
            ModelBinderDictionary modelBinders = this._dependencyInjector.GetService<ModelBinderDictionary>();
            KaiserConfigurationSection moduleConfig = this._dependencyInjector.GetService<KaiserConfigurationSection>();

            //ActionFilter注册表
            GlobalFilterCollection globalFilters = this._dependencyInjector.GetService<GlobalFilterCollection>();
            FilterRegistryFilterProvider filterRegistry = this._dependencyInjector.GetService<FilterRegistryFilterProvider>();
            globalFilters.Clear();
            filterRegistry.Clear();
            routes.Clear();

            //遍历配置文件中的modules节点，获取根据module配置信息创建实例并执行相关方法
            foreach (ModuleConfigurationElement module in moduleConfig.Modules)
            {
                IModule moduleInstance = moduleRegistry.Load(moduleConfig, module);

                if (moduleInstance != null)
                {
                    moduleInstance.Initialize();
                    moduleInstance.RegisterModelBinders(modelBinders);
                }
            }

            // 设置路由表
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            moduleRegistry.RegisterRoutes();

            //由于IXoohooModule.RegisterFilters方法内部可能会用到注册的路由信息(如RouteFilterCriteria)，所以该方法路由注册(RegisterRoutes)之后调用
            foreach (IModule module in moduleRegistry.GetModules())
                module.RegisterFilters(filterRegistry, globalFilters);
        }

        /// <summary>
        /// 引导程序清理
        /// </summary>
        /// <param name="bootStrapperState">引导程序状态</param>
        public void Cleanup(IDictionary<string, object> bootStrapperState)
        {
            _dependencyInjector.GetService<IModuleRegistry>().UnloadModules();
        }

        #endregion
    }
}
